## ── Attaching packages ───────────────────────────────────────── tidyverse 1.2.1 ──
## ✓ ggplot2 3.3.0 ✓ purrr 0.3.3
## ✓ tibble 2.1.3 ✓ dplyr 0.8.3
## ✓ tidyr 1.0.0 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.4.0
## ── Conflicts ──────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
Click here to be redirected to the source from which we obtained this dataset
Click here to be redirected to the direct source for this dataset
## here() starts at /Users/carriewright/Documents/GitHub/ocs-CO2/ocs-bloomberg-emissions-case-study
## # A tibble: 6 x 265
## country `1751` `1752` `1753` `1754` `1755` `1756` `1757` `1758` `1759`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Afghan… NA NA NA NA NA NA NA NA NA
## 2 Albania NA NA NA NA NA NA NA NA NA
## 3 Algeria NA NA NA NA NA NA NA NA NA
## 4 Andorra NA NA NA NA NA NA NA NA NA
## 5 Angola NA NA NA NA NA NA NA NA NA
## 6 Antigu… NA NA NA NA NA NA NA NA NA
## # … with 255 more variables: `1760` <dbl>, `1761` <dbl>, `1762` <dbl>,
## # `1763` <dbl>, `1764` <dbl>, `1765` <dbl>, `1766` <dbl>, `1767` <dbl>,
## # `1768` <dbl>, `1769` <dbl>, `1770` <dbl>, `1771` <dbl>, `1772` <dbl>,
## # `1773` <dbl>, `1774` <dbl>, `1775` <dbl>, `1776` <dbl>, `1777` <dbl>,
## # `1778` <dbl>, `1779` <dbl>, `1780` <dbl>, `1781` <dbl>, `1782` <dbl>,
## # `1783` <dbl>, `1784` <dbl>, `1785` <dbl>, `1786` <dbl>, `1787` <dbl>,
## # `1788` <dbl>, `1789` <dbl>, `1790` <dbl>, `1791` <dbl>, `1792` <dbl>,
## # `1793` <dbl>, `1794` <dbl>, `1795` <dbl>, `1796` <dbl>, `1797` <dbl>,
## # `1798` <dbl>, `1799` <dbl>, `1800` <dbl>, `1801` <dbl>, `1802` <dbl>,
## # `1803` <dbl>, `1804` <dbl>, `1805` <dbl>, `1806` <dbl>, `1807` <dbl>,
## # `1808` <dbl>, `1809` <dbl>, `1810` <dbl>, `1811` <dbl>, `1812` <dbl>,
## # `1813` <dbl>, `1814` <dbl>, `1815` <dbl>, `1816` <dbl>, `1817` <dbl>,
## # `1818` <dbl>, `1819` <dbl>, `1820` <dbl>, `1821` <dbl>, `1822` <dbl>,
## # `1823` <dbl>, `1824` <dbl>, `1825` <dbl>, `1826` <dbl>, `1827` <dbl>,
## # `1828` <dbl>, `1829` <dbl>, `1830` <dbl>, `1831` <dbl>, `1832` <dbl>,
## # `1833` <dbl>, `1834` <dbl>, `1835` <dbl>, `1836` <dbl>, `1837` <dbl>,
## # `1838` <dbl>, `1839` <dbl>, `1840` <dbl>, `1841` <dbl>, `1842` <dbl>,
## # `1843` <dbl>, `1844` <dbl>, `1845` <dbl>, `1846` <dbl>, `1847` <dbl>,
## # `1848` <dbl>, `1849` <dbl>, `1850` <dbl>, `1851` <dbl>, `1852` <dbl>,
## # `1853` <dbl>, `1854` <dbl>, `1855` <dbl>, `1856` <dbl>, `1857` <dbl>,
## # `1858` <dbl>, `1859` <dbl>, …
CO2_emissions <- CO2_emissions %>%
gather(key = Year, value = Emissions, -country) %>%
rename(Country=country) %>%
mutate(Emissions = Emissions/1000) %>%
rename(`CO2 Emissions (Mg)`=Emissions)
sapply(CO2_emissions, class)## Country Year CO2 Emissions (Mg)
## "character" "character" "numeric"
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1751 1817 1882 1882 1948 2014
ggplot(CO2_emissions, aes(x=Year, y=`CO2 Emissions (Mg)`, group=Country)) +
geom_line(alpha=0.2) +
labs(title = expression("Country CO"[2]*" Emissions per Year , 1751-2014"),
caption = expression("Limited to reporting countries")) +
ylab("Emissions (1M Metric Tonnes)")## Warning: Removed 33099 row(s) containing missing values (geom_path).
CO2_emissions %>%
group_by(Year) %>%
summarise(`CO2 Emissions (Mg)` = sum(`CO2 Emissions (Mg)`, na.rm = TRUE)) %>%
ggplot(aes(x=Year, y=`CO2 Emissions (Mg)`)) +
geom_line() +
labs(title = expression("World CO"[2]*" Emissions per Year , 1751-2014"),
caption = expression("Limited to reporting countries")) +
ylab("Emissions (1M Metric Metric Tonnes)")Click here to be redirected to the source from which we obtained this dataset
Click here to be redirected to the direct source for this dataset
Michael Need to determine units for this
## # A tibble: 6 x 220
## country `1801` `1802` `1803` `1804` `1805` `1806` `1807`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Afghan… NA NA NA NA NA NA NA
## 2 Albania 0.104 0.104 0.104 0.104 0.104 0.104 0.104
## 3 Algeria -0.00247 -0.00247 -0.00247 -0.00247 -0.00247 -0.00247 -0.00247
## 4 Andorra 0.166 0.166 0.166 0.166 0.166 0.166 0.166
## 5 Angola 0.425 0.425 0.425 0.425 0.425 0.425 0.425
## 6 Antigu… NA NA NA NA NA NA NA
## # … with 212 more variables: `1808` <dbl>, `1809` <dbl>, `1810` <dbl>,
## # `1811` <dbl>, `1812` <dbl>, `1813` <dbl>, `1814` <dbl>, `1815` <dbl>,
## # `1816` <dbl>, `1817` <dbl>, `1818` <dbl>, `1819` <dbl>, `1820` <dbl>,
## # `1821` <dbl>, `1822` <dbl>, `1823` <dbl>, `1824` <dbl>, `1825` <dbl>,
## # `1826` <dbl>, `1827` <dbl>, `1828` <dbl>, `1829` <dbl>, `1830` <dbl>,
## # `1831` <dbl>, `1832` <dbl>, `1833` <dbl>, `1834` <dbl>, `1835` <dbl>,
## # `1836` <dbl>, `1837` <dbl>, `1838` <dbl>, `1839` <dbl>, `1840` <dbl>,
## # `1841` <dbl>, `1842` <dbl>, `1843` <dbl>, `1844` <dbl>, `1845` <dbl>,
## # `1846` <dbl>, `1847` <dbl>, `1848` <dbl>, `1849` <dbl>, `1850` <dbl>,
## # `1851` <dbl>, `1852` <dbl>, `1853` <dbl>, `1854` <dbl>, `1855` <dbl>,
## # `1856` <dbl>, `1857` <dbl>, `1858` <dbl>, `1859` <dbl>, `1860` <dbl>,
## # `1861` <dbl>, `1862` <dbl>, `1863` <dbl>, `1864` <dbl>, `1865` <dbl>,
## # `1866` <dbl>, `1867` <dbl>, `1868` <dbl>, `1869` <dbl>, `1870` <dbl>,
## # `1871` <dbl>, `1872` <dbl>, `1873` <dbl>, `1874` <dbl>, `1875` <dbl>,
## # `1876` <dbl>, `1877` <dbl>, `1878` <dbl>, `1879` <dbl>, `1880` <dbl>,
## # `1881` <dbl>, `1882` <dbl>, `1883` <dbl>, `1884` <dbl>, `1885` <dbl>,
## # `1886` <dbl>, `1887` <dbl>, `1888` <dbl>, `1889` <dbl>, `1890` <dbl>,
## # `1891` <dbl>, `1892` <dbl>, `1893` <dbl>, `1894` <dbl>, `1895` <dbl>,
## # `1896` <dbl>, `1897` <dbl>, `1898` <dbl>, `1899` <dbl>, `1900` <dbl>,
## # `1901` <dbl>, `1902` <dbl>, `1903` <dbl>, `1904` <dbl>, `1905` <dbl>,
## # `1906` <dbl>, `1907` <dbl>, …
gdp_growth <- gdp_growth %>%
gather(key = Year, value = gdp_growth, -country) %>%
rename(Country=country) %>%
rename(`GDP Growth/Capita (%)` = gdp_growth) %>%
filter(complete.cases(.))
sapply(gdp_growth, class)## Country Year GDP Growth/Capita (%)
## "character" "character" "numeric"
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1801 1863 1915 1915 1967 2019
ggplot(gdp_growth, aes(x=Year, y=`GDP Growth/Capita (%)`, group=Country)) +
geom_line(alpha=0.2) +
labs(title = expression("Country GDP Growth per Capita per Year (Annual %), 1801-2019"),
caption = expression("Limited to reporting countries")) +
ylab("GDP Growth per Capita (Annual %)")gdp_growth %>%
group_by(Year) %>%
summarise(`GDP Growth/Capita (%)` = mean(`GDP Growth/Capita (%)`, na.rm = TRUE)) %>%
ggplot(aes(x=Year, y=`GDP Growth/Capita (%)`)) +
geom_line() +
labs(title = expression("Mean Country GDP Growth per Capita per Year (Annual %), 1801-2019"),
caption = expression("Limited to reporting countries")) +
ylab("GDP Growth per Capita (Annual %)")Click here to be redirected to the source from which we obtained this dataset
Click here to be redirected to the direct source for this dataset
## # A tibble: 6 x 57
## country `1960` `1961` `1962` `1963` `1964` `1965` `1966` `1967` `1968`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Albania NA NA NA NA NA NA NA NA NA
## 2 Algeria NA NA NA NA NA NA NA NA NA
## 3 Angola NA NA NA NA NA NA NA NA NA
## 4 Antigu… NA NA NA NA NA NA NA NA NA
## 5 Argent… NA NA NA NA NA NA NA NA NA
## 6 Armenia NA NA NA NA NA NA NA NA NA
## # … with 47 more variables: `1969` <dbl>, `1970` <dbl>, `1971` <dbl>,
## # `1972` <dbl>, `1973` <dbl>, `1974` <dbl>, `1975` <dbl>, `1976` <dbl>,
## # `1977` <dbl>, `1978` <dbl>, `1979` <dbl>, `1980` <dbl>, `1981` <dbl>,
## # `1982` <dbl>, `1983` <dbl>, `1984` <dbl>, `1985` <dbl>, `1986` <dbl>,
## # `1987` <dbl>, `1988` <dbl>, `1989` <dbl>, `1990` <dbl>, `1991` <dbl>,
## # `1992` <dbl>, `1993` <dbl>, `1994` <dbl>, `1995` <dbl>, `1996` <dbl>,
## # `1997` <dbl>, `1998` <dbl>, `1999` <dbl>, `2000` <dbl>, `2001` <dbl>,
## # `2002` <dbl>, `2003` <dbl>, `2004` <dbl>, `2005` <dbl>, `2006` <dbl>,
## # `2007` <dbl>, `2008` <dbl>, `2009` <dbl>, `2010` <dbl>, `2011` <dbl>,
## # `2012` <dbl>, `2013` <dbl>, `2014` <dbl>, `2015` <dbl>
energy_use <- energy_use %>%
gather(key = Year, value = energy_use, -country) %>%
rename(Country=country) %>%
rename(`Energy Use (kg, oil-eq./capita)` = energy_use)
sapply(energy_use, class)## Country Year
## "character" "character"
## Energy Use (kg, oil-eq./capita)
## "numeric"
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1960 1974 1988 1988 2001 2015
ggplot(energy_use, aes(x=Year, y=`Energy Use (kg, oil-eq./capita)`, group=Country)) +
geom_line(alpha=0.2) +
labs(title = expression("Country Energy Use (kg of Oil Equivalent per Capita), 1960-2015",),
caption = expression("Limited to reporting countries")) +
ylab("Energy Use (kg of Oil Equivalent per Capita)")## Warning: Removed 3203 row(s) containing missing values (geom_path).
energy_use %>%
group_by(Year) %>%
summarise(`Energy Use (kg, oil-eq./capita)` = sum(`Energy Use (kg, oil-eq./capita)`, na.rm = TRUE)) %>%
ggplot(aes(x=Year, y=`Energy Use (kg, oil-eq./capita)`)) +
geom_line() +
labs(title = expression("Worldwide Energy Use (kg of Oil Equivalent per Capita), 1960-2015"),
caption = expression("Limited to reporting countries")) +
ylab("Energy Use (kg of Oil Equivalent per Capita)")## New names:
## * `` -> ...3
## * `` -> ...4
## * `` -> ...5
## * `` -> ...6
## * `` -> ...7
## * … and 57 more problems
## # A tibble: 6 x 64
## `Data Source` `World Developm… ...3 ...4 ...5 ...6 ...7 ...8 ...9
## <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 Last Updated… 43819 <NA> <NA> <NA> <NA> <NA> <NA> <NA>
## 2 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
## 3 Country Name Country Code Indi… Indi… 1960 1961 1962 1963 1964
## 4 Aruba ABW Deat… SP.D… 6.38… 6.24… 6.11… 6.01… 5.91…
## 5 Afghanistan AFG Deat… SP.D… 32.2… 31.6… 31.0… 30.5… 30.0…
## 6 Angola AGO Deat… SP.D… 27.0… 26.8… 26.6… 26.4… 26.1…
## # … with 55 more variables: ...10 <chr>, ...11 <chr>, ...12 <chr>,
## # ...13 <chr>, ...14 <chr>, ...15 <chr>, ...16 <chr>, ...17 <chr>,
## # ...18 <chr>, ...19 <chr>, ...20 <chr>, ...21 <chr>, ...22 <chr>,
## # ...23 <chr>, ...24 <chr>, ...25 <chr>, ...26 <chr>, ...27 <chr>,
## # ...28 <chr>, ...29 <chr>, ...30 <chr>, ...31 <chr>, ...32 <chr>,
## # ...33 <chr>, ...34 <chr>, ...35 <chr>, ...36 <chr>, ...37 <chr>,
## # ...38 <chr>, ...39 <chr>, ...40 <chr>, ...41 <chr>, ...42 <chr>,
## # ...43 <chr>, ...44 <chr>, ...45 <chr>, ...46 <chr>, ...47 <chr>,
## # ...48 <chr>, ...49 <chr>, ...50 <chr>, ...51 <chr>, ...52 <chr>,
## # ...53 <chr>, ...54 <chr>, ...55 <chr>, ...56 <chr>, ...57 <chr>,
## # ...58 <chr>, ...59 <chr>, ...60 <chr>, ...61 <chr>, ...62 <chr>,
## # ...63 <chr>, ...64 <chr>
colnames(mortality) <- mortality[3,]
mortality <- mortality[-c(1:3),]
mortality <- mortality %>%
select(-`Country Code`,
-`Indicator Name`,
-`Indicator Code`) %>%
rename(Country="Country Name") %>%
gather(key = Year, value = `Deaths/1000 People`, -Country)
sapply(mortality, class)## Country Year Deaths/1000 People
## "character" "character" "character"
mortality$Year <- as.numeric(mortality$Year)
mortality$`Deaths/1000 People` <- as.numeric(mortality$`Deaths/1000 People`)
summary(mortality$Year)## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1960 1975 1990 1990 2004 2019
ggplot(mortality, aes(x=Year, y=`Deaths/1000 People`, group=Country)) +
geom_line(alpha=0.2) +
labs(title = expression("Country Crude Mortality Rate (per 1000 Persons), 1960-2019",),
caption = expression("Limited to reporting countries")) +
ylab("Crude Mortality Rate (per 1000 Persons)")## Warning: Removed 1477 row(s) containing missing values (geom_path).
mortality %>%
group_by(Year) %>%
summarise(`Deaths/1000 People` = mean(`Deaths/1000 People`, na.rm = TRUE)) %>%
ggplot(aes(x=Year, y=`Deaths/1000 People`)) +
geom_line() +
labs(title = expression("Mean Country Crude Mortality Rate (per 1000 Persons), 1960-2019",),
caption = expression("Limited to reporting countries")) +
ylab("Crude Mortality Rate (per 1000 Persons)")## Warning: Removed 2 row(s) containing missing values (geom_path).
## Parsed with column specification:
## cols(
## .default = col_double()
## )
## See spec(...) for full column specifications.
us_disaster <- us_disaster %>%
dplyr::select(Year,
`Drought Count`,
`Flooding Count`,
`Freeze Count`,
`Severe Storm Count`,
`Tropical Cyclone Count`,
`Wildfire Count`,
`Winter Storm Count`)
sapply(us_disaster, class)## Year Drought Count Flooding Count
## "numeric" "numeric" "numeric"
## Freeze Count Severe Storm Count Tropical Cyclone Count
## "numeric" "numeric" "numeric"
## Wildfire Count Winter Storm Count
## "numeric" "numeric"
## Parsed with column specification:
## cols(
## Date = col_double(),
## Value = col_double(),
## Anomaly = col_double()
## )
## Date Value Anomaly
## "numeric" "numeric" "numeric"
## [1] "Country" "Year" "CO2 Emissions (Mg)"
## [1] "Country" "Year" "GDP Growth/Capita (%)"
## [1] "Country" "Year"
## [3] "Energy Use (kg, oil-eq./capita)"
## [1] "Country" "Year" "Deaths/1000 People"
df_wide <- CO2_emissions %>%
full_join(gdp_growth, by=c("Country", "Year")) %>%
full_join(energy_use, by=c("Country", "Year")) %>%
full_join(mortality, by=c("Country", "Year"))
df_long <- df_wide %>%
gather(key=Indicator,value=Value,-Country, -Year) %>%
mutate(Region=case_when(Country=="United States" ~ "United States",
Country!="United States" ~ "Rest of the World"),
Type="Global")
setequal(sapply(df_long, class),sapply(us_disaster, class))## [1] TRUE
## [1] TRUE
## Country Year Indicator Value Region Type
## "character" "character" "character" "numeric" "character" "character"
df_long$Country <- as.factor(df_long$Country)
df_long$Year <- as.numeric(df_long$Year)
sapply(df_long,class)## Country Year Indicator Value Region Type
## "factor" "numeric" "character" "numeric" "character" "character"
ggplot(df_long, aes(x=Year, y=Value, group=Country)) +
geom_line(alpha=0.2) +
facet_grid(Indicator~., scales = "free_y") +
ylab("Indicator Value") +
labs(title="Distribution of Indicators by Year and Value")df_long %>%
filter(Type=="Global") %>%
group_by(Year,Indicator) %>%
tally() %>%
ggplot(aes(x=Year, y=n, color=Indicator)) +
geom_vline(xintercept = 1980, linetype=2, color="black") +
geom_vline(xintercept = 2010, linetype=2, color="black") +
geom_line() +
labs(title = "Countries with Complete Data per Year",
subtitle = "Global Data") +
ylab("Countries") +
scale_x_continuous(breaks = seq(1750,2020,by=10),
labels = seq(1750,2020,by=10)) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
axis.title.x = element_blank(),
legend.position = "bottom")df_long %>%
filter(Region=="United States") %>%
group_by(Indicator) %>%
summarise(Start=min(Year), End=max(Year)) %>%
ggplot(aes(x=Indicator, y=End)) +
geom_hline(yintercept = 1980, linetype=2, color="black") +
geom_hline(yintercept = 2010, linetype=2, color="black") +
geom_segment(aes(x=Indicator,
xend=Indicator,
yend=End,
y=Start)) +
geom_point(aes(x=Indicator, y=Start), shape=16, color="black") +
geom_point(aes(x=Indicator, y=End), shape=21, fill="white", color="black") +
coord_flip() +
labs(title = "Complete Data per Year",
subtitle = "US-specific Data") +
ylab("Countries") +
scale_y_continuous(breaks = seq(1750,2020,by=10),
labels = seq(1750,2020,by=10)) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
axis.title = element_blank())animation_1 <- df_long %>%
filter(Type=="Global") %>%
filter(Indicator=="Deaths/1000 People") %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
ggplot(aes(x=Year, y=Value, group=Country, color=Region, size=Region,alpha=Region)) +
geom_point() +
scale_color_manual(values = c("Red","Black")) +
scale_alpha_manual(values = c(0.1, 1)) +
scale_size_manual(values = c(0.25, 2)) +
labs(title="Distribution of Indicators by Year and Value, 1980-2010") +
theme(axis.text.x = element_text(angle = 90)) +
theme_classic() +
ylab("Crude Mortality Rate") +
transition_time(as.integer(Year)) +
shadow_wake(wake_length = 1, alpha = FALSE)
animate(animation_1, fps = 10, duration = 5)animation_2 <- df_long %>%
filter(Type=="Global") %>%
filter(Indicator=="Energy Use (kg, oil-eq./capita)") %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
ggplot(aes(x=Year, y=Value, group=Country, color=Region, size=Region, alpha=Region)) +
geom_point() +
scale_color_manual(values = c("Red","Black")) +
scale_alpha_manual(values = c(0.1, 1)) +
scale_size_manual(values = c(0.25, 2)) +
labs(title="Distribution of Indicators by Year and Value, 1980-2010") +
theme_classic() +
theme(axis.text.x = element_text(angle = 90)) +
ylab("Energy Use per Capita") +
transition_time(as.integer(Year)) +
shadow_wake(wake_length = 1, alpha = FALSE)
animate(animation_2, fps = 10, duration = 5)animation_3 <- df_long %>%
filter(Type=="Global") %>%
filter(Indicator=="GDP Growth/Capita (%)") %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
ggplot(aes(x=Year, y=Value, group=Country, color=Region, size=Region, alpha=Region)) +
geom_point() +
scale_color_manual(values = c("Red","Black")) +
scale_alpha_manual(values = c(0.1, 1)) +
scale_size_manual(values = c(0.25, 2)) +
labs(title="Distribution of Indicators by Year and Value, 1980-2010") +
theme_classic() +
theme(axis.text.x = element_text(angle = 90)) +
ylab("GDP Growth per Capita (%)") +
transition_time(as.integer(Year)) +
shadow_wake(wake_length = 1, alpha = FALSE)
animate(animation_3, fps = 10, duration = 5)animation_4 <- df_long %>%
filter(Type=="Global") %>%
filter(Indicator=="CO2 Emissions (Mg)") %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
ggplot(aes(x=Year, y=Value, group=Country, color=Region, size=Region, alpha=Region)) +
geom_point() +
scale_color_manual(values = c("Red","Black")) +
scale_alpha_manual(values = c(0.1, 1)) +
scale_size_manual(values = c(0.25, 2)) +
labs(title="Distribution of Indicators by Year and Value, 1980-2010") +
theme_classic() +
theme(axis.text.x = element_text(angle = 90)) +
ylab("CO2 Emissions (Mg)") +
transition_time(as.integer(Year)) +
shadow_wake(wake_length = 1, alpha = FALSE)
animate(animation_4, fps = 10, duration = 5)df_long %>%
filter(Type=="Global") %>%
filter(Indicator=="CO2 Emissions (Mg)") %>%
filter(Year>=1900) %>%
filter(Year<=2010) %>%
group_by(Country) %>%
mutate(max_val = max(Value)) %>%
ungroup() %>%
mutate(rank=dense_rank(-max_val)) %>%
filter(rank<=10) %>%
ggplot(aes(x=Year, y=fct_reorder(Country, Value, max))) +
geom_tile(color="transparent", aes(fill=log(Value))) +
scale_fill_gradientn(colors = c("yellow","red","black")) +
scale_x_continuous(breaks = seq(1900,2010,by=5),
labels = seq(1900,2010,by=5)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title = element_blank(),
legend.position = "bottom") +
labs(title = "Top 10 Emissions-producing Countries in 2010 (1900-2010)",
subtitle = "Ordered by Emissions Produced in 2010",
fill = "Ln(CO2 Emissions (Mg))")df_long_us <- df_long %>%
filter(Country=="United States")
# Approximated derivative function)
df_long_us <- df_long_us %>%
filter(Year>=1900,
Year<=2010) %>%
group_by(Indicator) %>%
mutate("Change (%)"=((Value/lag(Value))*100)-100,
Mean=mean(Value),
Anomaly=Value-Mean) %>%
ungroup() %>%
mutate(Anomaly_color=ifelse(Anomaly>0,"Positive",
ifelse(Anomaly<0,"Negative","Zero")),
Anomaly_color=factor(Anomaly_color, levels = c("Positive",
"Negative",
"Zero"),
ordered = TRUE))df_long_us %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
ggplot(aes(x=Year, y=Value)) +
geom_line() +
facet_wrap(Indicator~., ncol=2, nrow=3, scales = "free_y") +
scale_x_continuous(breaks = seq(1980,2010,by=5),
labels = seq(1980,2010,by=5)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title.y = element_blank()) +
labs(title = "US-specific Indicators (1980-2010)")df_long_us %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
ggplot(aes(x=Year, y=`Change (%)`, color=Indicator, fill="transparent")) +
geom_hline(yintercept=0.8, linetype=2) +
geom_hline(yintercept=1.2, linetype=2) +
geom_hline(yintercept = 1, linetype=3) +
geom_line(size=0.5) +
scale_x_continuous(breaks = seq(1980,2010,by=5),
labels = seq(1980,2010,by=5)) +
scale_y_continuous(breaks = seq(-500,1250, by=250),
labels = seq(-500,1250, by=250)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title.x = element_blank(),
legend.position = "bottom",
legend.direction = "horizontal") +
labs(title = "US-specific Indicators (1980-2010)",
subtitle = "Change (%) Lines")## Warning: Removed 1 row(s) containing missing values (geom_path).
df_long_us %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
filter(Indicator=="Temperature (Fahrenheit)"|
Indicator=="Energy Use (kg, oil-eq./capita)"|
Indicator=="CO2 Emissions (Mg)") %>%
ggplot(aes(x=Year, y=`Change (%)`, color=Indicator)) +
geom_hline(yintercept=0.8, linetype=2) +
geom_hline(yintercept=1.2, linetype=2) +
geom_hline(yintercept = 1, linetype=3) +
geom_line(size=1) +
scale_x_continuous(breaks = seq(1980,2010,by=5),
labels = seq(1980,2010,by=5)) +
scale_y_continuous(breaks = seq(-10,10, by=1),
labels = seq(-10,10, by=1),
limits = c(-10,10)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title.x = element_blank(),
legend.position = "bottom",
legend.direction = "horizontal") +
labs(title = "Emissions, Energy Use, and Temperature (1980-2010)",
subtitle = "Change (%) Lines")df_long_us %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
filter(Indicator=="Temperature (Fahrenheit)"|
Indicator=="Energy Use (kg, oil-eq./capita)"|
Indicator=="CO2 Emissions (Mg)") %>%
ggplot(aes(x=Year, y=`Change (%)`, color=Indicator)) +
geom_hline(yintercept=0.8, linetype=2) +
geom_hline(yintercept=1.2, linetype=2) +
geom_hline(yintercept = 1, linetype=3) +
geom_smooth(size=1, alpha=0.1, aes(fill=Indicator), se=FALSE) +
scale_x_continuous(breaks = seq(1980,2010,by=5),
labels = seq(1980,2010,by=5)) +
scale_y_continuous(breaks = seq(-10,10, by=1),
labels = seq(-10,10, by=1),
limits = c(-10,10)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title.x = element_blank(),
legend.position = "bottom",
legend.direction = "horizontal") +
labs(title = "US Emissions, Energy Use, and Temperatures (1980-2010)",
subtitle = "Smoothed Change (%) Lines")## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
df_long_us %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
filter(Indicator=="CO2 Emissions (Mg)"|
Indicator=="Temperature (Fahrenheit)") %>%
ggplot(aes(x=Year, y=Value)) +
geom_line() +
scale_x_continuous(breaks = seq(1980,2010,by=5),
labels = seq(1980,2010,by=5)) +
facet_wrap(Indicator~., scales = "free_y", ncol=1) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title = element_blank()) +
labs(title="US Emissions and Temperatures (1980-2010)")df_long_us %>%
filter(Indicator=="CO2 Emissions (Mg)"|
Indicator=="Temperature (Fahrenheit)") %>%
ggplot(aes(x=Year, y=Value)) +
geom_vline(xintercept = 1980, linetype=2, color="black") +
geom_vline(xintercept = 2010, linetype=2, color="black") +
geom_segment(aes(x=Year, y=Value, xend=Year, yend=Mean,color=Anomaly_color), size=1.25) +
scale_color_manual(values = c("red","blue","gray")) +
geom_hline(aes(yintercept=Mean), linetype=1, color="black") +
scale_x_continuous(breaks = seq(1900,2010,by=5),
labels = seq(1900,2010,by=5)) +
facet_wrap(Indicator~., scales = "free_y", ncol=1) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title = element_blank(),
legend.position = "none") +
labs(title = "US Emissions and Temperatures (1900-2010)",
subtitle = "Indicator Mean Represented by Solid Black Line")df_long_us %>%
filter(Indicator=="Temperature (Fahrenheit)"|
Indicator=="CO2 Emissions (Mg)") %>%
ggplot(aes(x=Year, y=`Change (%)`)) +
annotate("rect", xmin=-Inf, xmax=Inf, ymin=0, ymax=Inf, alpha=0.25, fill="green") +
annotate("rect", xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=0, alpha=0.25, fill="red") +
geom_hline(yintercept=0, linetype=1) +
geom_segment(aes(x=Year, y=`Change (%)`, xend=Year, yend=0), size=1.25) +
facet_wrap(Indicator~., scales = "free_y", ncol=1) +
scale_x_continuous(breaks = seq(1900,2010,by=5),
labels = seq(1900,2010,by=5)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title.x = element_blank(),
legend.position = "bottom",
legend.direction = "horizontal") +
labs(title = "US Emissions, Energy Use, and Temperatures (1980-2010)",
subtitle = "Change (%) Lines")## Warning: Removed 2 rows containing missing values (geom_segment).